package com.ruoyi.controller;

import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.utils.R;
import com.ruoyi.utils.UpLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController()
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @Value("${path.upload}")
    private String path;
    @Autowired
    private UpLoadUtil upLoadUtil;

    @PostMapping("/upload")
    public void upLoad(MultipartFile []file){
        for (int i = 0;i<file.length;i++){
            String name = file[i].getOriginalFilename();
            String endName = name.substring(name.lastIndexOf("."));
            String name2 = UUID.randomUUID()+endName;
            String name1 = path+name2;
            File file1 = new File(name1);
            try {
                file[i].transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                FileInputStream fileInputStream = new FileInputStream(file1);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                FileOutputStream fileOutputStream = new FileOutputStream(name);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                String s = "";
                if ((s = bufferedReader.readLine()) == null) {
                    bufferedWriter.write(s);
                }
                System.out.println(name2);
                bufferedWriter.flush();
                bufferedWriter.close();
                bufferedReader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
